package com.factory.iamp.pojo;

import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class TableAssembly {
    private int aid;
    private int lid;
    private String name;
    private String title;
    private String description;
    private String creator;
    private int header_id;
    private List<Integer> body_list;
    private int tail_id;
    private Timestamp created_at;

    public TableAssembly(JSONObject data) {
        this.SetAll(data);
    }

    public void SetAll(JSONObject data) {
        if(data.containsKey("name")) this.name = data.getString("name");
        if(data.containsKey("lid")) this.lid = data.getInteger("lid");
        if(data.containsKey("title")) this.title = data.getString("title");
        if(data.containsKey("description")) this.description = data.getString("description");
        if(data.containsKey("creator")) this.creator = data.getString("creator");
        if(data.containsKey("header_id")) this.header_id = data.getInteger("header_id");
        if(data.containsKey("body_list")) this.setBody_list(data.getString("body_list"));
        if(data.containsKey("tail_id")) this.tail_id = data.getInteger("tail_id");
    }

    public void setBody_list(String value) {
        if(value.length() < 1) this.body_list = new ArrayList<>();
        else this.body_list = JSONObject.parseArray(value, Integer.class);
    }

    public boolean insertCheck() {
        if(this.body_list == null) this.body_list = new ArrayList<>();
        return this.name == null || this.name.length() < 1;
    }
}
